<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@page import="db.tables.DBWorkTable"%>
<%@page import="db.DBMain"%>
<%@page import="html.RequestParser"%>
<%@page import="db.tables.DBTypeTable"%>
<%@page import="db.tables.reference.DBGroupMemberTable"%>
<%@page import="java.sql.Date"%>
<%@page import="db.tables.DBGroupTable"%>
<%
	String file = "./index.jsp?"+Constant.CONTENT+"="+Constant.group, method = "get";
%>
<%
	RequestParser.send_action_file = file;
	RequestParser.send_method = method;
%>
<%@include file="town.jsp" %>
<script type="text/javascript"><!--

<%
Long type_id_temp=DBMain.getTypeTable().selectLong(DBTypeTable.TYPE_ID_COL,DBTypeTable.TYPE_NAME_COL,Constant.Type.GROUP),user_id=0L;//FIXME the user_id has to be set correct! session.getAttribute(Constant.USERID);
session.setAttribute(Constant.Content.TYPE,type_id_temp);

RequestParser groupParser=new RequestParser(request,file,method);
groupParser.isAdmin(admin);
groupParser.setAdd(Constant.Content.ADD_GROUP);
groupParser.setContent(Constant.Content.GROUP);
groupParser.setSubmit(Constant.Content.SUBMIT_GROUP);
groupParser.setDelete(Constant.Content.DELETE_GROUP);
groupParser.setInput(new String[]{Constant.Content.INPUT_GROUP,Constant.Content.INPUT_GROUP_HOMEPAGE,Constant.Content.TOWN,Constant.Content.CATEGORY});
groupParser.setInsert_columns(new String[]{DBGroupTable.GROUP_NAME_COL,DBGroupTable.HOMEPAGE_COL,DBGroupTable.TOWN_ID_COL,DBGroupTable.CATEGORY_ID_COL});
groupParser.setDisplay_columns(new String[]{DBGroupTable.GROUP_ID_COL,DBGroupTable.GROUP_NAME_COL,DBGroupTable.HOMEPAGE_COL});
groupParser.setPk_column(DBGroupTable.GROUP_ID_COL);
groupParser.setTable(DBMain.getGroupTable());
groupParser.setFk_column(DBGroupTable.TOWN_ID_COL);
groupParser.setFk_id(town_id);
groupParser.process();

RequestParser groupMemberParser=new RequestParser(request,file,method);
groupMemberParser.isAdmin(admin);
groupMemberParser.setAdd(Constant.Content.ADD_GROUP_MEMBER);
groupMemberParser.setContent(Constant.Content.GROUP_MEMBER);
groupMemberParser.setSubmit(Constant.Content.SUBMIT_GROUP_MEMBER);
groupMemberParser.setDelete(Constant.Content.DELETE_GROUP_MEMBER);
groupMemberParser.setInput(new String[]{Constant.Content.GROUP,Constant.Content.INPUT_START,Constant.Content.INPUT_END});
groupMemberParser.setInsert_columns(new String[]{DBGroupMemberTable.USER_ID_COL,DBGroupMemberTable.GROUP_ID_COL,DBGroupMemberTable.START_DATE_COL,DBGroupMemberTable.END_DATE_COL});
groupMemberParser.setDisplay_columns(new String[]{DBGroupMemberTable.GROUP_MEMBER_ID_COL,DBGroupMemberTable.START_DATE_COL,DBGroupMemberTable.END_DATE_COL});
groupMemberParser.setPk_column(DBGroupMemberTable.GROUP_MEMBER_ID_COL);
groupMemberParser.setTable(DBMain.getGroupMemberTable());
groupMemberParser.setFk_column(DBGroupMemberTable.USER_ID_COL);
groupMemberParser.setFk_id(user_id);
groupMemberParser.setPk_id(RequestParser.getLongFromParameter(request, groupMemberParser.getContent()));

	if(admin) {
		if (request.getParameter(groupMemberParser.getSubmit()) != null) {
			if (RequestParser.areAllParametersNotNull(request, groupMemberParser.getInput())) {
				int i=0;
				Object[] result = new Object[groupMemberParser.getInsert_columns().length];
				//DBGroupMemberTable.USER_ID_COL 
				result[i]=user_id;
				//DBGroupMemberTable.GROUP_ID_COL
				i++;
				result[i]=Long.parseLong(request.getParameter(Constant.Content.GROUP));
				//DBGroupMemberTable.START_DATE_COL,
				i++;
				result[i]=Date.valueOf(request.getParameter(Constant.Content.INPUT_START));
				//DBGroupMemberTable.END_DATE_COL
				String t=request.getParameter(Constant.Content.INPUT_END);
				i++;
				if(t!=null&&!t.equals("")&&!t.equalsIgnoreCase("null"))
					result[i]=Date.valueOf(t);
				else result[i]=null;
				groupMemberParser.setPk_id(groupMemberParser.getTable().insertGetPK(groupMemberParser.getInsert_columns(), result));
			} else if (RequestParser.areSomeParametersNotNull(request, groupMemberParser.getInput())) {
				// Error output because only one parameter is set -
				// needed are both!
			}
		}
	}

	if(admin) {
		if (request.getParameter(groupMemberParser.getDelete()) != null && groupMemberParser.getPk_id() != null) {
			groupMemberParser.getTable().delete(groupMemberParser.getPk_column(), groupMemberParser.getPk_id());
		}
	}

	groupMemberParser.setArray(groupMemberParser.getTable().getSelect(groupMemberParser.getDisplay_columns(), new String[]{groupMemberParser.getFk_column()}, new Object[]{groupMemberParser.getFk_id()}));
%>
//
--></script>
<form name="<%=groupParser.getContent() %>" action="<%=groupParser.getSend_action_file() %>" method="<%=groupParser.getSend_method()%>">
	<%@include file="category_only.jsp" %>
	<select name="<%=Constant.Content.GROUP %>" onchange="submit()">
		<%
			groupParser.printOptionList(out);
		%>
	</select>
          <table width="38%" border="0">
       <tr> 
         <td width="17%">Beginn:</td>
         <td width="83%">
           <input type="text" name="<%=Constant.Content.INPUT_START%>" size="11" value="">
		<a href="javascript: void(0);" onmouseover="if (timeoutId) clearTimeout(timeoutId);window.status='Show Calendar';return true;" 
		onmouseout="if (timeoutDelay) calendarTimeout();window.status='';" onclick="g_Calendar.show(event,'<%=groupParser.getContent() %>.beginn',true,'yyyy-mm-dd'); return false;">
		<img src="../images/calendar.gif" name="imgCalendar" width="34" height="21" border="0" alt=""></a>
         </td>
             <tr> 
         <td width="17%">Ende: </td>
         <td width="83%">
           <input type="text" name="<%=Constant.Content.INPUT_END%>" size="11" value="">
		<a href="javascript: void(0);" onmouseover="if (timeoutId) clearTimeout(timeoutId);window.status='Show Calendar';return true;" 
		onmouseout="if (timeoutDelay) calendarTimeout();window.status='';" onclick="g_Calendar.show(event,'<%=groupParser.getContent() %>.ende',true,'yyyy-mm-dd'); return false;">
		<img src="../images/calendar.gif" name="imgCalendar" width="34" height="21" border="0" alt=""></a>
         </td>
             <tr>
         <td width="17%">
         	<input type="hidden" name="<%=Constant.Content.CATEGORY %>" value="<%=categoryParser.getPk_id() %>"/>
         	<input type="hidden" name="<%=groupParser.getContent() %>" value="<%=groupParser.getPk_id()%>"/>
         	<input type="hidden" name="<%=Constant.Content.COUNTRY %>" value="<%=country_id %>"/>
			<input type="hidden" name="<%=Constant.Content.TOWN %>" value="<%=town_id %>"/> 
           <input type="submit" name="<%=Constant.Content.SUBMIT_GROUP_MEMBER %>" value="Speichern">
         </td>
         <td width="83%">
           <input type="reset" name="zuruecksetzen" value="Zur&uuml;cksetzen">
         </td>
     </table>
</form>